import { createRouter, createWebHistory } from 'vue-router';
import HomeView from './views/HomeView.vue';
import SpaceView from './views/SpaceView.vue';
import ChatView from './views/ChatView.vue';
import DiyView from './views/DiyView.vue'
import Buy from './views/buy/index.vue'
import LoginView from './views/LoginView.vue';
import OrderView from './views/OrderView.vue';
import CartView from './views/order/cartList.vue';
import ServiceView from './views/ServiceView.vue';
import DetailGoods from'./views/buy/components/DetailGoods.vue'
const routes = [
    {
        path: '/',
        name: 'Home',
        component: HomeView
    }
    ,
    {
        path: '/home',
        name: 'Home',
        component: HomeView
    },
    {
        path: '/space',
        name: 'Space',
        component: SpaceView
    },
    {
        path: '/login',
        name: 'Login',
        component: LoginView
    },
    {
        path: '/chat',
        name: 'Chat',
        component: ChatView
    },
    {
        path: '/diy',
        name: 'Diy',
        component: DiyView
    },
    {
        path: '/buy',
        name: 'Buy',
        component: Buy
    },
    {
        path:'/checkout',
        component: () => import('./views/checkout/index.vue')
    },
    {
        path:'/detail/:id',
        component:DetailGoods
    },
    {
        path: '/order',
        name: 'Order',
        component: OrderView,
    },
    {
        path: '/cart',
        name: 'Cart',
        component: CartView
    },
    {
        path: '/pay',
        component: () => import('./views/pay/index.vue') 
    },
    {
        path: '/service',
        name: 'Service',
        component: ServiceView
    }
]

// 添加重定向规则
const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 全局前置守卫，用于重定向
router.beforeEach((to, from, next) => {
    if (to.path === '/') {
        next({ path: '/home' });
    } else {
        next();
    }
});

export default router